﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using System;
using XCI.Extensions;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows.Controls;
using XCI.Windows.Extensions;
using XCI.Windows.Helper;

namespace XCI.Sys.Forms
{
    public partial class DicManagerControl : BaseXtraUserControl
    {
        /// <summary>
        /// 字典对象
        /// </summary>
        private SysDicCategory category;

        /// <summary>
        /// 表格操作类
        /// </summary>
        private GridHelper<SysDic> helper;

        public DicManagerControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 页面初始化
        /// </summary>
        private void InitForm()
        {
            //1.初始化表格操作类
            helper = new GridHelper<SysDic>(gridView);
            helper.ElapsedTimeItem = labExecuteTime;

            //2.绑定控件数据
            BeginInit(() =>
            {
                //选中第一项
                statusCombo.SelectedIndex = 1;
            });

            ButtonStatus();
            labExecuteTime.Caption = null;
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        public void SetDicModel(SysDicCategory p_category)
        {
            category = p_category;
            if (category != null)
            {
                LoadData();
            }
            else
            {
                throw new ArgumentNullException(nameof(category));
            }
        }

        /// <summary>
        /// 数据加载
        /// </summary>
        private void LoadData()
        {
            //1.获取查询条件
            var key = editKey.Text.Trim();
            var status = statusCombo.SelectedValue.ToBoolOrNull();

            helper.RunCallback(e =>
            {
                e.Result = DicService.Instance.SelectList(category.Code, key, status);
            })
            .CompletCallback(e =>
            {
                ButtonStatus();
            })
            .RunAsync();
        }

        /// <summary>
        /// 数据编辑
        /// </summary>
        /// <param name="created">是否新建</param>
        /// <param name="model">模型对象</param>
        private void Edit(bool created, SysDic model)
        {
            if (category == null) throw new ArgumentNullException(nameof(category));
            var editForm = new FrmDicEdit();
            editForm.SetModel(created, category, model);
            editForm.ShowDialogOk(LoadData);
        }

        /// <summary>
        /// 获取选中的对象
        /// </summary>
        /// <returns>返回选中的对象</returns>
        private SysDic GetSelected()
        {
            return DicService.Instance.SelectById(helper.SelectedId);
        }

        /// <summary>
        /// 设置按钮可用状态
        /// </summary>
        private void ButtonStatus()
        {
            btnNew.Enabled = true;
            btnEdit.Enabled = !helper.HasMultiSelected && helper.HasSelected;
            btnCopy.Enabled = !helper.HasMultiSelected && helper.HasSelected;
            btnDelete.Enabled = helper.HasSelected;
            btnExport.Enabled = helper.HasData;
        }

        /// <summary>
        /// 页面加载事件
        /// </summary>
        private void DicManagerControl_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
            {
                InitForm();
            }
        }

        /// <summary>
        /// 表格双击事件
        /// </summary>
        private void gridView_DoubleClick(object sender, EventArgs e)
        {
            helper.DblClickRow(e, () =>
            {
                var detailsForm = new FrmDicDetails(helper);
                detailsForm.ShowDialogOk();
            });
        }

        /// <summary>
        /// 表格选中项发生变化事件
        /// </summary>
        private void gridView_SelectionChanged(object sender, DevExpress.Data.SelectionChangedEventArgs e)
        {
            ButtonStatus();
        }

        private void gridView_EndSorting(object sender, EventArgs e)
        {
            helper.View.BeginDataUpdate();
            LoadData();
            helper.View.EndDataUpdate();
        }

        /// <summary>
        /// 关键字文本框键盘事件
        /// </summary>
        private void editName_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            //回车事件回调
            EnterHandle(e, LoadData);
        }

        /// <summary>
        /// 搜索按钮点击事件
        /// </summary>
        private void btnSearch_Click(object sender, EventArgs e)
        {
            //加载首页数据
            LoadData();
        }

        /// <summary>
        /// 状态发生变化事件
        /// </summary>
        private void statusCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            EndInit(LoadData);
        }

        /// <summary>
        /// 新建事件
        /// </summary>
        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Edit(true, DicService.Instance.New());
        }

        /// <summary>
        /// 修改事件
        /// </summary>
        private void btnEdit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Edit(false, GetSelected());
        }

        /// <summary>
        /// 复制事件
        /// </summary>
        private void btnCopy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Edit(true, DicService.Instance.Copy(GetSelected()));
        }

        /// <summary>
        /// 删除事件
        /// </summary>
        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            helper.Delete(() => { DicService.Instance.Delete(helper.SelectedIds).IfTrue(() => { LoadData(); }); });
        }

        private void btnExport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var key = editKey.Text.Trim();
            var status = statusCombo.SelectedValue.ToBoolOrNull();
            ExcelExportCore(() => DicService.Instance.Export(category.Code, key, status), $"系统数据字典 - {category.Name}");
        }
    }
}